package com.itheima.controller;

import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 学生管理
 */
@RequestMapping("/students")
@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 分页查询
     *
     * @param name
     * @param degree
     * @param clazzId
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public Result list(String name, Integer degree, Integer clazzId,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "5") Integer pageSize) {
        PageResult pageResult = studentService.list(name, degree, clazzId, page, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable Integer[] ids) {
        studentService.delete(ids);
        return Result.success();
    }

    /**
     * 添加
     *
     * @param student
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Student student) {
        studentService.add(student);
        return Result.success();
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result queryById(@PathVariable Integer id) {
        Student student = studentService.queryById(id);
        return Result.success(student);
    }

    /**
     * 修改
     *
     * @param student
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Student student) {
        studentService.update(student);
        return Result.success();
    }

    /**
     * 修改学生违纪分数
     *
     * @param id
     * @param score
     * @return
     */
    @PutMapping("/violation/{id}/{score}")
    public Result updateViolation(@PathVariable Integer id, @PathVariable Integer score) {
        studentService.updateViolation(id, score);
        return Result.success();
    }


}
